const Sequelize = require("sequelize");

// const { dbName, host, port, user, password } =
//   require("../config/config.dev").database;

  const { dbName, host, port, user, password } = require(`../config/config.${
    process.env.NODE_ENV || "dev"
  }`).database;

const sequelize = new Sequelize(dbName, user, password, {
  dialect: "mysql",
  host: host,
  port: port,
  logging: (message) => {
    console.log(message);
  },
  timezone: "+08:00",
  dialectOptions: {
    charset: "utf8mb4",
    dateStrings: true,
    typeCast: true,
  },
  define: {
    // create_time && update_time
    timestamps: true,
    // delete_time
    paranoid: true,
    createdAt: "created_at",
    updatedAt: "updated_at",
    deletedAt: "deleted_at",
    // 把驼峰命名转换为下划线
    underscored: true,
    freezeTableName: true,
    charset: "utf8mb4",
    collate: "utf8mb4_general_ci",
    scopes: {
      bh: {
        attributes: {
          exclude: ["updated_at", "deleted_at", "created_at"],
        },
      },
    },
  },
});

// 创建模型
sequelize.sync({
  force: false,
});

module.exports = {
  sequelize,
};
